package com.hrt.datasender.web;

import java.util.ArrayList;
import java.util.List;

import com.hrt.datasender.datacollect.drive.opc.OpcDataClient;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.service.BcSendPlatformService;
import com.hrt.datasender.datasent.DataSenderSerial;
import com.hrt.datasender.datasent.onenet.OneNetSender;
import com.hrt.datasender.datasent.udp.UdpSender;
import javafish.clients.opc.JOpc;
import javafish.clients.opc.browser.JOpcBrowser;
import org.eclipse.jetty.servlet.DefaultServlet;

import com.btcode.common.MyLog;
import com.btcode.common.PropertiesUnit;
import com.btcode.log.ILog;
import com.btcode.schedule.ScheduleManager;
import com.btcode.web.core.server.DefaultServer;
import com.btcode.web.mvc.servlet.ApiPackScanInfo;
import com.hrt.datasender.datacollect.DataCollectMain;
import com.hrt.datasender.dataschedule.DataSchedulerMain;
import com.hrt.datasender.datasent.DataSender;
import com.hrt.datasender.datastorage.StorageMain;
import com.hrt.datasender.mq.MQMain;
import com.hrt.log.MqMonitorLogFactory;

public class WebMain {

    private static int WEBPORT = 8090;
    private static ILog log;
    public static final String VERSION = "V1.1.0";
    public static String DBTYPE;

    public static void main(String[] args) throws Exception {

        if (args == null || args.length <= 0) {
            throw new Exception("缺少配置文件路径");
        }

        String propertiesPath = args[0];

        System.out.println("配置文件路径：" + propertiesPath);

        PropertiesUnit.setPropertiesPath(propertiesPath);

        //读取webPort端口
        String webPort = PropertiesUnit.getValue("webPort");
        if(webPort!=null){
            WEBPORT = Integer.parseInt(webPort);
        }


        /**
         * 读取数据库连接类型
         */
        DBTYPE = PropertiesUnit.getValue("conf", "dbType");

        start(propertiesPath);

        // 每次启动程序的时候，打开指定网址
        // String url =
        // "http://localhost:8088/scy/webResource/view/sw/index.html";
        // Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " +
        // url);
    }

    private static void startJetty() throws Exception {

        String projectName = PropertiesUnit.getValue("webProjectName");
        String logPath = PropertiesUnit.getValue("logPath");
        String webBasePath = PropertiesUnit.getValue("webBasePath");

        List<ApiPackScanInfo> controllerPackets = new ArrayList<>();
        controllerPackets.add(new ApiPackScanInfo("com.hrt.datasender.web", WebMain.class));

        DefaultServer server = new DefaultServer(WEBPORT, logPath + "/jetty.log", webBasePath, controllerPackets,
                projectName);

        server.addServlet(new DefaultServlet(), "/log/*");

        server.startServer();
    }

    private static void start(String propertiesPath) throws Exception {

        String logPath = PropertiesUnit.getValue("logPath");

        MyLog.getInstance().setRootPath(logPath);

        String sendMode = PropertiesUnit.getValue("sendMode");

        /**
        /**
         * 使用MQ组合日志，把日志发送到消息队列中，方便后续监控
         */
        MyLog.getInstance().setLogFactory(new MqMonitorLogFactory());

        log = MyLog.getInstance().getLogger(WebMain.class.getName());

        try {
            try {
                startJetty();
                log.info("web管理程序启动成功");
            }
            catch (Exception e) {
                log.error("web管理程序启动失败", e);
            }

            /**
             * 初始化opc
             */
            String isReadOpc = PropertiesUnit.getValue("isReadOpc");
            if("true".equals(isReadOpc)){
                OpcDataClient.initOpc();
            }

            try {
                MQMain.start();
                log.info("MQ模块启动成功");
            }
            catch (Exception e) {
                log.error("MQ模块启动失败", e);
            }

            try {
                // DataSchedulerMain.start(propertiesPath);
                DataSchedulerMain.start(logPath);
                log.info("调度模块启动成功");
            }
            catch (Exception e) {
                log.error("调度模块启动失败", e);
            }

            try {
                DataCollectMain dataCollect = new DataCollectMain();
                dataCollect.start();
                log.info("采集模块启动成功");
            }
            catch (Exception e) {
                log.error("采集模块启动失败", e);
            }

            try {
                StorageMain storageMain = new StorageMain();
                storageMain.start();
                log.info("存储模块启动成功");
            }
            catch (Exception e) {
                log.error("存储模块启动失败", e);
            }

            switch (sendMode){
                case "0":
                    try {
                        DataSender.start();
                        log.info("TCP发送模块启动成功");
                    } catch (Exception e) {
                        log.error("TCP发送模块启动失败",e);
                    }
                    break;
                case "1":
                    try {
                        DataSenderSerial.start();
                        log.info("DTU发送模块启动成功");
                    } catch (Exception e) {
                        log.error("DTU发送模块启动失败",e);
                    }
                    break;
                case "2":
                    try {
                        OneNetSender oneNetSender = new OneNetSender();
                        oneNetSender.start();
                        log.info("NB-IOT发送模块启动成功");
                    } catch (Exception e) {
                        log.error("NB-IOT发送模块启动失败",e);
                    }
                    break;
                case "3":
                    try {
                        UdpSender udpSender = new UdpSender();
                        udpSender.start();
                        log.info("UDP发送模块启动成功");
                    } catch (Exception e) {
                        log.error("UDP发送模块启动失败",e);
                    }
                    break;
                default:
                    try {
                        DataSender.start();
                        log.info("TCP发送模块启动成功");
                    } catch (Exception e) {
                        log.error("TCP发送模块启动失败",e);
                    }
                    break;
            }

            try {
                ScheduleManager.getInstance().start();
                log.info("定时模块启动成功");
            }
            catch (Exception e) {
                log.error("定时模块启动失败", e);
            }

            log.info("主程序启动成功.");

        }
        catch (Exception e) {
            log.error("主程序启动失败", e);
        }

    }

}
